﻿<Window xmlns:my="clr-namespace:KegManager"  x:Class="KegManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        
        Title="MainWindow" Height="768" Width="1024" Loaded="Window_Loaded" StateChanged="Window_StateChanged" x:Name="mainWindow" Closing="mainWindow_Closing" MouseMove="mainWindow_MouseMove">
    <Window.Resources>
        <ObjectDataProvider x:Key="settings" ObjectType="{x:Type my:Properties.Settings}" />
        <my:PathLabelConverter x:Key="PathLabelConverter" />
        <my:UserImageConverter x:Key="UserImageConverter" />
        <my:FormattingConverter x:Key="FormattingConverter" />
        <my:TemperatureConverter x:Key="TemperatureConverter" />
        <my:TemperatureRangeConverter x:Key="TemperatureRangeConverter" />
        <my:SmallVolumeConverter x:Key="SmallVolumeConverter" />
        <my:SmallVolumeUnitConverter x:Key="SmallVolumeUnitConverter" />
        <my:LargeVolumeConverter x:Key="LargeVolumeConverter" />
        <DataTemplate x:Key="TapTemplate">
            <my:TapControl x:Name="tap" Keg="{Binding Keg}" />
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="KegDock" Value="Left" TargetName="tap"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="KegDock" Value="Right" TargetName="tap"></Setter>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="TemperatureBlockTemplate">
            <Border BorderBrush="Black" BorderThickness="0" Height="70" Width="150" Margin="2,2,2,2">
                <Grid Margin="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Viewbox Grid.Row="0" Grid.Column="0">
                        <TextBlock Text="{Binding Path=Value, Converter={StaticResource TemperatureConverter}, ConverterParameter=#0.00}" UseLayoutRounding="True">
                            <TextBlock.Foreground>
                                <MultiBinding Converter="{StaticResource TemperatureRangeConverter}">
                                    <Binding Path="MaximumAlert"/>
                                    <Binding Path="MinimumAlert"/>
                                    <Binding Path="Value"/>
                                </MultiBinding> 
                            </TextBlock.Foreground>
                            <!--<TextBlock FontFamily="Segoe UI">°</TextBlock>-->
                        </TextBlock>
                    </Viewbox>
                    <Border Grid.Row="1" Grid.Column="0" BorderBrush="Black" BorderThickness="0,0,0,0">
                        <TextBlock Text="{Binding Name}" FontSize="14" TextAlignment="Center"/>
                    </Border>
                </Grid>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="UserListTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=AvatarImage, Converter={StaticResource UserImageConverter}}" Width="50"/>
                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="24"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="PourTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=User.AvatarImage, Converter={StaticResource UserImageConverter}}" Width="100"/>
                <TextBlock>
                    <StackPanel Height="100">
                        <TextBlock Text="{Binding Path=Time, Converter={StaticResource FormattingConverter}, ConverterParameter=\{0:d\}}"/>
                        <TextBlock Text="{Binding Path=Time, Converter={StaticResource FormattingConverter}, ConverterParameter=\{0:t\}}"/>
                    </StackPanel>
                    <TextBlock Text="{Binding Path=User.Name}" FontWeight="Bold" FontSize="16"/> poured
                    <TextBlock Text="{Binding Path=Volume, Converter={StaticResource SmallVolumeConverter}, ConverterParameter=#0.00}" FontWeight="Bold" FontSize="16">
                        <TextBlock Text="{Binding Source={StaticResource settings}, Path=Default.VolumeUnit, Mode=OneWay, Converter={StaticResource SmallVolumeUnitConverter}, ConverterParameter=short}" />
                    </TextBlock>
                    of
                    <TextBlock Text="{Binding Path=Keg.Beer.Name}" FontWeight="Bold" FontSize="16"/>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <TabControl Name="MainTab" SelectionChanged="MainTab_SelectionChanged" TabStripPlacement="Bottom">
            <TabItem Header="Taps" Name="tabTaps">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="100"/>
                    </Grid.RowDefinitions>
                    <ItemsControl Grid.Row="1" Name="listTemperatures" HorizontalContentAlignment="Center" HorizontalAlignment="Center" BorderThickness="0"
                                  ItemsSource="{Binding ElementName=mainWindow, Path=RecentTemperatures}"
                                  ItemTemplate="{StaticResource TemperatureBlockTemplate}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                    <Viewbox Grid.Row="0" Margin="0,0">
                        <ItemsControl Grid.Row="1" Name="listTaps" HorizontalContentAlignment="Center" HorizontalAlignment="Center" BorderThickness="0" Focusable="False"
                                    ItemsSource="{Binding ElementName=mainWindow, Path=ActiveTaps}"
                                    ItemTemplate="{StaticResource TapTemplate}"
                                    AlternationCount="2"
                                    Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ScrollContentPresenter}}}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </Viewbox>
                </Grid>
            </TabItem>
            <TabItem Header="Pours" Name="tabPours">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="1">
                        <Button Name="buttonModifyPour" IsEnabled="False">Modify Pour</Button>
                        <Button Name="buttonRemovePour" IsEnabled="False">Remove Pour</Button>
                    </StackPanel>
                    <ListBox Grid.Column="1" Grid.Row="0" Margin="10" Name="listPours" HorizontalContentAlignment="Stretch" 
                              ItemTemplate="{StaticResource PourTemplate}">
                    </ListBox>
                </Grid>
            </TabItem>
            <TabItem Header="Kegs" Name="tabKegs">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="30" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="1">
                        <Button Name="buttonNewKeg" Click="Button_Click_2">New Keg</Button>
                        <Button Name="buttonModifyKeg" IsEnabled="False" Click="buttonModifyKeg_Click">ModifyKeg</Button>
                        <Button Name="buttonDeleteKeg" IsEnabled="False" Click="buttonDeleteKeg_Click">Delete Keg</Button>
                        <Button Name="buttonSetTap1" IsEnabled="False" Click="buttonSetTap1_Click">Set Keg on Tap 1</Button>
                        <Button Name="buttonSetTap2" IsEnabled="False" Click="buttonSetTap2_Click">Set Keg on Tap 2</Button>
                    </StackPanel>
                    <!--<Button Grid.Column="1" Grid.Row="0" Command="ScrollBar.LineLeftCommand" CommandTarget="{Binding ElementName=awesomeitem}">L</Button>-->
                    <ListBox Name="listKegs" Grid.Column="2" Grid.Row="0" SelectionChanged="listKegs_SelectionChanged" ItemsSource="{Binding}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                    <StackPanel Orientation="Horizontal"  Margin="5,5,5,5" >

                                        <my:KegControl Value="{Binding Path=CurrentVolume}" ShowPints="Hidden" FillOpacity=".5" Height="150"/>
                                        <!--<ProgressBar Width="20" Maximum="{Binding Path=InitialVolume}" Value="{Binding Path=CurrentVolume}"  Minimum="0" Orientation="Vertical" />-->
                                        <Image  Height="150" Source="{Binding Path=Beer.Label, Converter={StaticResource PathLabelConverter}}"/>
                                        <StackPanel >
                                            <TextBlock Text="{Binding Path=Beer.Name}" FontSize="24" FontWeight="Bold" />
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="150"/>
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>
                                                <TextBlock Grid.Column="0" Grid.Row="0">Initial Volume:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=InitialVolume, Converter={StaticResource LargeVolumeConverter}}" />
                                                <TextBlock Grid.Column="0" Grid.Row="1">Current Volume:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Path=CurrentVolume, Converter={StaticResource LargeVolumeConverter}}" />
                                                <TextBlock Grid.Column="0" Grid.Row="2" >Fill Date:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding Path=Filled}" />
                                            </Grid>
                                        </StackPanel>
                                    </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </TabItem>
            <TabItem Header="Beers" Name="tabBeers">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="0" Grid.Row="1">
                        <Button Name="buttonNewBeer" Click="buttonNewBeer_Click">New Beer</Button>
                        <Button Name="buttonModifyBeer" IsEnabled="False" Click="buttonModifyBeer_Click">Modify Beer</Button>
                        <Button Name="buttonDeleteBeer" IsEnabled="False" Click="buttonDeleteBeer_Click">Delete Beer</Button>
                    </StackPanel>
                    <ListBox Grid.Column="1" Grid.Row="0" Margin="10" Name="listBeers" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="listBeers_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">
                                    <Grid Margin="5">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                        </Grid.RowDefinitions>

                                        <Image Grid.Row="0" Grid.Column="0" Source="{Binding Path=Label, Converter={StaticResource PathLabelConverter}, Mode=OneWay}" Height="100"/>
                                        <StackPanel Grid.Row="0" Grid.Column="1" Margin="5,5,5,5">
                                            <TextBlock Text="{Binding Path=Name}" FontSize="24" FontWeight="Bold" />
                                            <TextBlock Text="{Binding Path=Description}" TextWrapping="Wrap" />
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="100"/>
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>
                                                <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">Original Gravity:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=Gravity}" />
                                                <TextBlock Grid.Column="0" Grid.Row="1" FontWeight="Bold">ABV:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Path=Alcohol}" />
                                                <TextBlock Grid.Column="0" Grid.Row="2" FontWeight="Bold">Bitterness:</TextBlock>
                                                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding Path=Bitterness}" />
                                            </Grid>
                                        </StackPanel>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </TabItem>
            <TabItem Header="Temperatures" Name="tabTemperatures">
                <Label Height="28" HorizontalContentAlignment="Center" >Not Implemented</Label>
            </TabItem>
            <TabItem Header="Users" Name="tabUsers">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="300" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <DockPanel Name="UserDisplay"  Grid.Column="1" Visibility="Hidden"> 
                                <Grid DockPanel.Dock="Top" Margin="5">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="200" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="30"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding Path=AvatarImage, Converter={StaticResource UserImageConverter}}" Width="200" Height="200"/>
                                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}" FontWeight="Bold" FontSize="24"/>
                                    <TextBlock Grid.Row="1" Grid.Column="1" Text="Total Poured: " FontWeight="Bold" FontSize="14">
                                        <TextBlock Text="{Binding Path=TotalVolumePoured, Converter={StaticResource LargeVolumeConverter}, ConverterParameter=#0.00}" />
                                        <TextBlock Text="{Binding Source={StaticResource settings}, Path=Default.VolumeUnit}" />
                                    </TextBlock>
                                    <TextBlock Grid.Row="2" Grid.Column="1" Text="in " FontWeight="Bold" FontSize="14">
                                        <TextBlock Text="{Binding Path=Pours.Count}" />
                                        <TextBlock Text=" different pours" />
                                    </TextBlock>
                                </Grid>
                                <ListView Name="listUserPours" DockPanel.Dock="Left" ItemsSource="{Binding Path=Pours}">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Width="150" Header="Date" DisplayMemberBinding="{Binding Time}"/>
                                            <GridViewColumn Width="50" Header="Volume" DisplayMemberBinding="{Binding Path=Volume, Converter={StaticResource SmallVolumeConverter}, ConverterParameter=#0.00}" />
                                            <GridViewColumn Width="200" Header="Beer" DisplayMemberBinding="{Binding Keg.Beer.Name}" />
                                        </GridView>
                                    </ListView.View>

                                </ListView>
                            <StackPanel DockPanel.Dock="Right" Grid.Column="1">

                            </StackPanel>
                    </DockPanel>
                    <DockPanel Grid.Column="0" VerticalAlignment="Stretch">
                        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5,5,5,5">
                            <Button>New User</Button>
                            <Button Margin="10,0,0,0">Edit User</Button>
                        </StackPanel>
                        <ListBox Grid.Row="0" Margin="10,0" Name="listUsers" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch"
                              ItemTemplate="{StaticResource UserListTemplate}" SelectionChanged="listUsers_SelectionChanged">
                        </ListBox>
                    </DockPanel>
                </Grid>
            </TabItem>
            <TabItem Header="Control" Name="tabControl">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="0" Grid.Column="0">
                        <GroupBox Header="KegManager">
                            <StackPanel Width="120" HorizontalAlignment="Left" Margin="2,2,2,2">
                                <Button Click="Button_Click">Toggle Fullscreen</Button>
                                <Button Click="Button_Click_1" Margin="0,10,0,0">Exit</Button>
                            </StackPanel>
                        </GroupBox>
                        <GroupBox Header="Database" Height="180">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="25"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Label Grid.Row="0" Grid.Column="0" Content="Server:" HorizontalAlignment="Right" />
                                <Label Grid.Row="1" Grid.Column="0" Content="Database:" HorizontalAlignment="Right"/>
                                <Label Grid.Row="2" Grid.Column="0" Content="User:" HorizontalAlignment="Right" />
                                <Label Grid.Row="3" Grid.Column="0" Content="Password:" HorizontalAlignment="Right"/>
                            
                                <TextBox Name="txtServer" Grid.Row="0" Grid.Column="1" Margin="2" />
                                <TextBox Name="txtDatabase" Grid.Row="1" Grid.Column="1" Margin="2" />
                                <TextBox Name="txtUser" Grid.Row="2" Grid.Column="1" Margin="2" />
                                <TextBox Name="txtPassword" Grid.Row="3" Grid.Column="1" Margin="2"/>
                            
                                <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4" HorizontalAlignment="Right">
                                    <Button Click="Button_TestDatabase">Test Connection</Button>
                                    <Button  Margin="10,0,0,0" Width="50" Click="Button_ApplyDatabase">Apply</Button>
                                    <Button  Margin="10,0,0,0" Width="50" Click="Button_RevertDatabase">Revert</Button>
                                </StackPanel>
                            </Grid>
                        </GroupBox>
                        <GroupBox Header="Data">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="65"/>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="40"/>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0" Content="Data Path" HorizontalContentAlignment="Right"/>
                                <TextBox Grid.Column="1" Name="txtDataDirectory"></TextBox>
                                <Button Grid.Column="2" Margin="2,2,2,0" Click="ApplyDataDirectory_Click" Height="22">Apply</Button>
                            </Grid>
                        </GroupBox>
                    </StackPanel>
                    <StackPanel Grid.Row="0" Grid.Column="1">
                        <GroupBox Header="Units">
                            <Grid HorizontalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="150"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="30"/>
                                </Grid.RowDefinitions>
                                <Label Grid.Column="0" Grid.Row="0" Content="Temperature" HorizontalContentAlignment="Right"/>
                                <ComboBox Grid.Column="1" Grid.Row="0" Margin="2,2,2,2" Name="cmbTemperatureUnit" SelectionChanged="ChangeUnits">
                                    <ComboBoxItem Content="Celcius" />
                                    <ComboBoxItem Content="Fahrenheit" />
                                </ComboBox>
                                <Label Grid.Column="0" Grid.Row="1" Content="Volume" HorizontalContentAlignment="Right" />
                                <ComboBox Grid.Column="1" Grid.Row="1" Margin="2,2,2,2" Name="cmbVolumeUnit" SelectionChanged="ChangeUnits">
                                    <ComboBoxItem Content="Liter" />
                                    <ComboBoxItem Content="Gallon" />
                                </ComboBox>
                            </Grid>
                        </GroupBox>
                    </StackPanel>
                </Grid>
            </TabItem>
            <TabItem Header="Test" Name="tabTest">
                <!--<Viewbox>
                    <StackPanel Orientation="Horizontal">
                        <my:TapControl x:Name="bacon1"/>
                        <my:TapControl x:Name="bacon2"/>
                    </StackPanel>
                </Viewbox>-->
                <StackPanel>
                    <my:ThermometerControl />
                    <TextBlock  FontSize="48">25</TextBlock>
                    
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</Window>